package cn.zhuama.org.service.impl;

import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.domain.Department;
import cn.zhuama.org.mapper.DepartmentMapper;
import cn.zhuama.org.query.DepartmentQuery;
import cn.zhuama.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IDepartmentServiceImpl implements IDepartmentService {
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.save(department);
    }

    @Override
    @Transactional
    public void del(Long id) {
        departmentMapper.del(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department queryOne(Long id) {
        return departmentMapper.queryOne(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery query) {
        Long count = departmentMapper.queryCount(query);
        if(count<1){
            return new PageList<Department>();
        }
        List<Department> list = departmentMapper.queryData(query);
        return new PageList<Department>(count,list);
    }
}
